package com.turing.post.webservice.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.InitialContext;

import weblogic.workarea.PrimitiveContextFactory;
import weblogic.workarea.PropagationMode;
import weblogic.workarea.WorkContext;
import weblogic.workarea.WorkContextMap;

import com.turing.post.account.util.MasterAccount;
import com.turing.post.account.util.MasterAccountDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * @author zxp
 * @Jan 22, 2013
 */
public class WscBackup {
	
	@EJB(mappedName = "account.impl.MasterAccountDaoImpl")
	private MasterAccountDao masterAccountDaoImpl;

	public static String getWeblogicDomainDirectory() {
		String wlsDomainDir = System.getProperty("weblogic.domainDir");
		if (wlsDomainDir == null || "".equals(wlsDomainDir)) {
			try {
				InitialContext ctx = new InitialContext();
				MBeanServer server = (MBeanServer) ctx
						.lookup("java:comp/env/jmx/runtime");
				ObjectName service = new ObjectName(
						"com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
				ObjectName domain = (ObjectName) server.getAttribute(service,
						"DomainConfiguration");
				wlsDomainDir = (String) server.getAttribute(domain,
						"RootDirectory");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return wlsDomainDir;
	}

	public void A_setnull(BigInteger accno) throws PCPSApplicationException {
		WorkContextMap map;
		System.out.println(getWeblogicDomainDirectory());
		try {
			map = (WorkContextMap) new InitialContext()
					.lookup("java:comp/WorkContextMap");
			WorkContext stringContext = PrimitiveContextFactory
					.create("A_setnull");
			map.put("PCPSCONTEXT", stringContext, PropagationMode.GLOBAL);
		} catch (Exception e) {
		}
		MasterAccount ac = masterAccountDaoImpl
				.getMasterAccountByMasterAccNo(accno);
		PCPSUtil.Log("cardno:" + ac.getCardNo());
		ac.setCardNo(null);
		ac.setCloseStatus("A");
		masterAccountDaoImpl.update(ac);
		PCPSUtil.Log("after cardno: " + ac.getCardNo());
	}

}
